﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Cors;
using Brc_PaaS.Api.Common;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Core.Redis;
using Brc_PaaS.Common.Model.Redis;
using System.Collections.Generic;
using StackExchange.Redis;
using Brc_PaaS.Bll.System;
using System.ComponentModel.DataAnnotations;

namespace Brc_PaaS.Api.Controllers.Redis
{
    /// <summary>
    /// 批量操作Redis控制器
    /// </summary>
    [Route("api/redis/[controller]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName ="Redis")]
    public class RedisBatchController : RedisBaseController
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="redis"></param>
        /// <param name="system"></param>
        public RedisBatchController(IConfiguration configuration,Brc_PaaS.Bll.Redis.IRedis redis,ISystem system) : base(configuration, redis, system)
        {

        }
        /// <summary>
        /// 批量删除Key(哈希，集合，列表的操作也是调用此方法)
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult<BaseResponseModel> Delete([Required(ErrorMessage = "键集合不能为空")]List<string> keys)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && keys != null)
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.IsSuccess = redisManager.KeyDelete(keys) > 0;
                }
            }
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }
        /// <summary>
        /// 批量插入String
        /// </summary>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<BaseResponseModel> Post([Required(ErrorMessage = "参数不能为空")]List<RedisStringModel> keyValues)
        {
            BaseResponseModel result = new BaseResponseModel() { IsSuccess = false, Msg = string.Empty };
            if (SystemInfo != null && BusinessRedisServerEntity != null && keyValues != null)
            {
                var list = new List<KeyValuePair<RedisKey, RedisValue>>();
                keyValues.ForEach(i =>
                {
                    list.Add(new KeyValuePair<RedisKey, RedisValue>(i.Key, i.Value));
                });
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    result.IsSuccess = redisManager.StringSet(list);
                }
            }
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }
        /// <summary>
        /// 批量获取String
        /// </summary>
        /// <param name="listKey"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ResponseModel<List<string>>> Get([Required(ErrorMessage = "键集合不能为空")]List<string> listKey)
        {
            ResponseModel<List<string>> result = new ResponseModel<List<string>>() { IsSuccess = false, Msg = string.Empty, Data = new List<string>() };
            if (SystemInfo != null && BusinessRedisServerEntity != null && listKey != null)
            {
                using (IRedisManager redisManager = new RedisManager(BusinessRedisServerEntity))
                {
                    var redisValueArray = redisManager.StringGet(listKey);
                    if (redisValueArray != null)
                    {
                        foreach (var item in redisValueArray)
                        {
                            result.Data.Add(item.ToString());
                        }
                    }
                }
            }
            result.IsSuccess = true;
            result.Msg = result.IsSuccess ? "操作成功" : "操作失败";
            return Ok(result);
        }

    }
}